
export default {
	data() {
		return {
			truntableData: [], // 转盘数据
			color: ["#014EB5","#A800FF","#e82edb","#B5454C","#443bff","#e8a425","#ff7aab","#e84b1e","#552ce2","#e8861e","#d441ff","#9c7aff","#014EB5","#A800FF","#e82edb","#B5454C","#443bff","#e8a425","#ff7aab","#e84b1e","#552ce2","#e8861e","#d441ff","#9c7aff","#014EB5","#A800FF","#e82edb","#B5454C","#443bff","#e8a425","#ff7aab","#e84b1e","#552ce2","#e8861e","#d441ff","#9c7aff","#014EB5","#A800FF","#e82edb","#B5454C","#443bff","#e8a425","#ff7aab","#e84b1e","#552ce2","#e8861e","#d441ff","#9c7aff","#014EB5","#A800FF","#e82edb","#B5454C","#443bff","#e8a425","#ff7aab","#e84b1e","#552ce2","#e8861e","#d441ff","#9c7aff","#014EB5","#A800FF","#e82edb","#B5454C","#443bff","#e8a425","#ff7aab","#e84b1e","#552ce2","#e8861e","#d441ff","#9c7aff"],
			timerRun: false, // 游戏是否已开始
			transform: null, 
			id:0,
			sys:{},
			get_img:this.$get_img,
			infor:{}
		}
	},
	props: {
		infors:Object,
		index:Number,//中奖
		list: Array, // 转盘基础数据
		show: Boolean, // 显示组件：默认不显示
		// showModel: Boolean, // 显示模式：弹窗（默认），正常
	},
	mounted() {
		this.sys=uni.getStorageSync("sys")
		this.get_infor()
		this.init();
	},
	methods: {
		// 隐藏组件
		close(){
			if(this.timerRun){
				uni.showToast({
					title: '请耐心等待',
					duration: 1500,
					icon: "none"
				});
				return
			}
			this.$emit("turntableState", false);
		},
		get_infor() {
			let that = this
			this.$model.get("user/get_infor").then(res => {
				if (res.code == 200) {
					uni.setStorageSync("infor", res.data)
					that.infor = res.data
				}
			})
		},
		bgFunc() {
			return this.sys.draw_bg != '' ? this.sys.draw_bg : this.sys.sys_background
		},
		// 点击开始按钮
		
		go(){
			let that=this
			if(this.timerRun){
				uni.showToast({
					title: '请耐心等待',
					duration: 1500,
					icon: "none"
				});
				return
			}
			
			if(this.infors.draw_times<1){
				uni.showToast({
					title:'抽奖次数不够，请观看项目获取奖励',
					duration:1000
				})
				setTimeout(function(){
					uni.navigateTo({
						url:'../house/house?type=draw'+"&id="+that.sys.chick_nav
					})
				},1000)
				return;
			}
			
			that.timerRun = true;
			this.$model.get("user/common/draw_run").then(res=>{
				if(res.code==200){
					
					that.get_infor()
					this.id=res.data.id
					//let power = Math.floor(Math.random()*100 + 1); //随机获取一个力量值
					let power=0
					power=res.data.power
					let jiaodu = 0; // 旋转的角度
					let decline = 0.01; // 衰退
					let timer = setInterval(()=>{
						if(Number(power.toFixed(2)) <= 0){
							clearInterval(timer);
							this.getData(jiaodu%360);
						}
						jiaodu += power;

						// #ifdef H5
						document.getElementById("rotateBoard").style.transform = 'rotate('+ jiaodu +'deg)';
						// #endif
						// #ifdef MP-WEIXIN || APP-PLUS
						this.transform = 'rotate('+ jiaodu +'deg)'
						// #endif
						
						power = (power - power*decline) < decline?0:power - power*decline;
					},10)
				}
				
			})
			
		},
		// 通过旋转得角度，获取当前指向得奖品
		getData(deg){
			let _this = this;
			deg = 360 - deg;
			let prize = null;
			let nums = this.list.length;
			for(let i = 0; i < nums; i++){
				if(this.list[i].start <= deg && deg < this.list[i].start + this.list[i].prop*5){
					prize = this.list[i];
					
					if(prize.id==_this.id){
						this.$emit("getPrize", prize.state, prize.id, prize.reward)
					}else{
						this.$emit("getPrize", prize.state, prize.id, "很遗憾")
					}
					let msg="谢谢参与"
					if(prize.reward){
						msg="恭喜获得"+prize.reward+"!\n请到售楼部领取"
					}
					
					
					uni.showModal({
						title: "奖励",
						content: msg,
						showCancel: false,
						success: function (res) {
							if (res.confirm) {
								// #ifdef H5
								document.getElementById("rotateBoard").style.transform = 'rotate('+ 0 +'deg)';
								// #endif
								// #ifdef MP-WEIXIN || APP-PLUS
								_this.transform = 'rotate('+ 0 +'deg)'
								// #endif
								_this.timerRun = false;
							}
						}
					});
					break;
				}
			}
			
		},
		init(){
			let tempStart = 0;
			let tempScale = 0;
			this.list.forEach((item, i)=>{
				item["scale"] = item.prop*5 - 90; // 所占弧度的度数
				item["scaleText"] = tempScale + (90 + (item.prop*5 - 90))/2; // 文字的回旋角度
				item["sin"] = Math.sin(Math.PI*2*item.scaleText/360)*160; // 正弦
				item["cos"] = Math.cos(Math.PI*2*item.scaleText/360)*160; // 余弦
				tempScale = tempScale + 90 + (item.prop*5 - 90);
				if(i !== 0){
					tempStart = tempStart + this.list[i - 1].prop*5;
				}
				item["start"] = tempStart;
			});
			this.truntableData = this.list;
			
		},
	}
}